package com.example.campusrear.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.campusrear.Service.PhotoCollectionService;
import com.example.campusrear.Utils.OssUtil;
import com.example.campusrear.Utils.R;
import com.example.campusrear.entity.PhotoCollection;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/photo")
public class PhotoCollectionController {
    @Resource
    private PhotoCollectionService photoCollectionService;

    @PostMapping("/save")
    public R<String> save(@RequestBody PhotoCollection photoCollection){
        photoCollectionService.save(photoCollection);
        return R.success("添加成功");
    }

    @GetMapping("/queryPhoto")
    public R<List<PhotoCollection>> query(Long mId){
        LambdaQueryWrapper<PhotoCollection> queryWrapper = new LambdaQueryWrapper<>();
        if(mId!=null){
            queryWrapper.eq(PhotoCollection::getId,mId);
        }
        List<PhotoCollection> list = photoCollectionService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/File")
    public String uploadAvatar(@RequestParam MultipartFile multipartFile) throws IOException {
//        1:调用ossutil的upload
        if (multipartFile.getSize() > 0) {
            return OssUtil.upload(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
        }
        return "上传成功";
    }
}
